# from abc import ABCMeta, abstractmethod
#
# class Abstac(metaclass=ABCMeta):
#     @abstractmethod
#     def draw(self):
#         pass
# # 叶子组件
# class Poin(Abstac):
#     def __init__(self, poin):
#         self.poin = poin
#     def __str__(self):
#         return "%s点" % self.poin
#     def draw(self):
#         print(str(self))
#
# # 叶子组件
# class Lin(Abstac):
#     def __init__(self, p1, p2):
#         self.p1 = p1
#         self.p2 = p2
#     def __str__(self):
#         return "%s,%s组成线段" % (self.p1, self.p2)
#     def draw(self):
#         print(str(self))
# # 简单组合模型
# # a = Lin(Poin(2), Poin(10))
# # print(a)
#
# # 复合组件
# class Comp(Abstac):
#     def __init__(self, iterable):
# # 创建child列表接收数据
#         self.child = []
#         for c in iterable:
# # 调用方法追加入child
#             self.adds(c)
#     def adds(self, grapsh):
#         self.child.append(grapsh)
#     def draw(self):
#         print("----复合图形----")
# # 从child列表中循环调用draw方法
#         for g in self.child:
#             g.draw()
#         print("----复合图形----")
#
# #复合组成(树状结构)
# b = Poin(12)
# a = Lin(Poin(2), Poin(10))
# c = Lin(Poin(44), Poin(9))
# com = Comp([b, a, c])
# com.draw()

# 代理模式
from abc import ABCMeta, abstractmethod


class Age(metaclass=ABCMeta):
    @abstractmethod
    def open_fi(self):
        pass

    @abstractmethod
    def close_fi(self, content):
        pass


# 文件写入——远端代理
class Realsub(Age):
    def __init__(self, filename):
        self.filename = filename
        f = open(self.filename, 'r', encoding='utf-8')
        self.content = f.read()
        f.close()

    def open_fi(self):
        return self.content

    def close_fi(self, content):
        f = open(self.filename, 'w', encoding='utf-8')
        f.write(content)
        f.close()


# 虚代理(调用时判断状态，open_fi不需要打开，节省内存空间)
class VirturlPro(Age):
    def __init__(self, filename):
        self.filename = filename
        self.subj = None

    def open_fi(self):
        if not self.subj:
            self.subj = Realsub(self.filename)
        return self.subj.open_fi()

    def close_fi(self, content):
        pass


# 保护代理（设置访问权限）
class ProtectPor(Age):
    def __init__(self, filename):
        self.subj = Realsub(filename)

    def open_fi(self):
        return self.subj.open_fi()

    def close_fi(self, content):
        raise PermissionError("无写入权限")

# sub = Realsub('books.txt')
# sub.open_fi()
# sub1 = VirturlPro('books.txt')
# sub1.open_fi()
# sub = ProtectPor('books.txt')
# sub.close_fi("abc")
